package com.holly.unit.deform.api.factory;

import cn.hutool.core.util.StrUtil;
import com.holly.unit.core.util.StrKitUtil;
import com.holly.unit.deform.api.pojo.tool.DesformWidget;
import com.holly.unit.deform.api.util.ConvertUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述: select-user | select-depart 组件用到的转换器
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/14 14:45
 */
public class HollyBizConvert extends ForeseeConvert{

    /**
     * @param widget
     * @param convertFields 指定哪些字段需要翻译
     */
    public HollyBizConvert(DesformWidget widget, String table, String txt, List<String> convertFields) {
        this.model = widget.getKey();
        String customReturnField = widget.getOptions().getCustomReturnField();
        String code = null;
        for (String field : convertFields) {
            if (field.equalsIgnoreCase(customReturnField)) {
                // 特殊处理 orcCode
                if (ConvertUtil.DEPART_ORG_CODE_NAME.equalsIgnoreCase(field)) {
                    // 驼峰转下划线，转大写
                    code = StrKitUtil.camelToUnderline(field).toUpperCase();
                } else {
                    code = field;
                }
                break;
            }
        }
        if (code == null) {
            // 其他自定义返回字段不转换
            this.dictList = new ArrayList<>();
            return;
        }
//        ISysBaseAPI sysBaseAPI = SpringContextUtils.getBean(ISysBaseAPI.class);
//        this.dictList = sysBaseAPI.queryTableDictItemsByCode(table, txt, code);
        // TODO 查询字典列表
        this.dictList = null;
    }

    @Override
    public String converterToVal(String txt) {
        if (StrUtil.isEmpty(txt)) {
            return null;
        }
        List<String> res = new ArrayList<>();
        for (String temp : txt.split(",")) {
            Object switchValue = super.converterToVal(temp);
            if (switchValue == null) {
                continue;
            }
            res.add(switchValue.toString());
        }
        return String.join(",", res);
    }

    @Override
    public String converterToTxt(String val) {
        if (StrUtil.isEmpty(val)) {
            return null;
        }
        List<String> res = new ArrayList<>();
        for (String temp : val.split(",")) {
            Object switchTxt = super.converterToTxt(temp);
            if (switchTxt == null) {
                continue;
            }
            res.add(switchTxt.toString());
        }
        return String.join(",", res);
    }

}
